<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/8
 * Time: 9:51
 */

namespace App\Http\Controllers\API\LifeCycle\Package;

use App\Http\Controllers\Controller;
use App\Http\Resources\PackageDetailResource;
use App\Http\Resources\PackageResource;
use App\Http\Resources\PackageStoreResource;
use App\Services\LifeCycle\BrandService;
use App\Services\LifeCycle\CategoryService;
use App\Services\LifeCycle\PackageService;
use App\Services\LifeCycle\StoreService;
use Illuminate\Http\Request;

class PackageController extends Controller
{
    public function index(Request $request)
    {
        $param = $request->all();

        $package = PackageService::all($param);

        $category = CategoryService::one(1);

        $brand = BrandService::one($category->brand_id);

        $package_resource = PackageResource::collection($package);

        $give_attr = PackageService::giveAttr($package_resource);

        if ($package_resource) {
            return $this->success([
                'category' => $category,
                'brand' => $brand,
                'package' => $package_resource,
                'give' => $give_attr
            ]);
        } else {
            return $this->failure();
        }
    }

    public function detail(Request $request)
    {
        $param = $request->all();

        $package = PackageService::one($param['package_id']);

        return $this->success(new PackageDetailResource($package));
    }

    public function settlement(Request $request)
    {
        $param = $request->all();

        $package = new PackageDetailResource(PackageService::one($param['package_id']));

        $store = StoreService::storeSelect($package->brand_id);

        return $this->success(['package' => $package, 'store' => $store]);
    }
}